import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBCameraSelectPopPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBBaseModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBWorkTaiModule/YBFenXiangHuKePage/YBBaseProvider/YBFengMianImageEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBAPi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBHttpUtils.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBWorkTaiNetWork/YBBaseWorkTaiNetWorkManager.dart';

class YBWenZhangZhengshiFengMianWeChatPage extends StatefulWidget {

  final String weChatUrlStr;
  final String title;
  final String contentStr;
  const YBWenZhangZhengshiFengMianWeChatPage({Key key, this.weChatUrlStr,this.title,this.contentStr}) : super(key: key);

  @override
  _YBWenZhangZhengshiFengMianWeChatPageState createState() => _YBWenZhangZhengshiFengMianWeChatPageState(weChatUrlStr: weChatUrlStr,titleStr: title,contentStr: contentStr);
}

class _YBWenZhangZhengshiFengMianWeChatPageState extends State<YBWenZhangZhengshiFengMianWeChatPage> {

  final String weChatUrlStr;
  final String titleStr;
  final String contentStr;
  _YBWenZhangZhengshiFengMianWeChatPageState({this.weChatUrlStr,this.titleStr,this.contentStr});

  TextEditingController _editingController = TextEditingController();

  String _contentStr;
  String _imageUrl;
  String _title;
  String _orgin;

  @override
  void initState() {
    super.initState();
    if(weChatUrlStr != null){
      _getUrlInfo();
    }else{
      _title = titleStr;
      _contentStr = contentStr;
    }

    fengMianImageEventBus.on<YBFengMianImageEventBus>().listen((event) {
        print('封面结果------${event.fengMianImageStr}---object');
        setState(() {
          _imageUrl = "${YBApi.Base_image_URL}/${event.fengMianImageStr}";
        });
    });

  }


  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text(
            YBChineseText.workTaiZhuanFaWenZhangTitle,
            style: YBTextStyle.TextStyle30313320Medium,
          ),
          elevation: 0,
        ),
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Container(
                height: YBTextStyle.adapterSize200,
                child:Padding(
                  padding: EdgeInsets.only(left:YBTextStyle.adapterSize20,right:YBTextStyle.adapterSize20),
                  child: Stack(
                    children: [
                      Container(
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: CachedNetworkImageProvider(_imageUrl??""),
                            fit: BoxFit.cover,
                          )
                        ),
                      ),
                      Container(
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize20)),
                          border: Border.all(color: YBColor.white,width: 1),
                        ),
                        child: Align(
                          alignment: Alignment.center,
                          child: FlatButton(
                            child: Text(YBChineseText.workTaiXieWenZhangFengMianTitle,style: YBTextStyle.TextStyleFFFFFF16Medium,),
                            onPressed: () async {
                              YBCameraSelectPopPage.getCameraHuangMianSelectPop(context);
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                height: YBTextStyle.adapterSize15,
              ),
              Container(
                height: YBTextStyle.adapterSize60,
                child: Padding(
                  padding: EdgeInsets.only(left:YBTextStyle.adapterSize20,right:YBTextStyle.adapterSize20),
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize8)),
                      color: YBColor.colorF5F5F7,
                    ),
                    child: Padding(
                      padding: EdgeInsets.only(left: YBTextStyle.adapterSize15,right: YBTextStyle.adapterSize15),
                      child: Row(
                        children: [
                          Container(
                            child: Text(YBChineseText.workTaiWenZhangLaiYuanTitle,style: YBTextStyle.TextStyle18181816Medium,),
                          ),
                          Container(
                            width: YBTextStyle.adapterSize10,
                          ),
                          Expanded(
                            child: TextField(
                              textAlign: TextAlign.end,
                              decoration: InputDecoration(
                                hintText: YBChineseText.workTaiInputWenZhangNameTitle,
                                border: InputBorder.none,
                              ),
                              controller: _editingController,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              Container(
                height: YBTextStyle.adapterSize15,
              ),
              Container(
                height: YBTextStyle.adapterSize44,
                child: Padding(
                  padding: EdgeInsets.only(left:YBTextStyle.adapterSize20,right:YBTextStyle.adapterSize20),
                  child:RawMaterialButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize8)),
                      ),
                      child: Text(YBChineseText.workTaiEnterPushlishWenZhangTitle,style: YBTextStyle.TextStyleFFFFFF16Medium,),
                      fillColor: YBColor.commonThemeColor,
                      onPressed:()async{
                        _getWenZhangeInfo(_contentStr,_title,_imageUrl, _editingController.text, weChatUrlStr);
                      }),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _getUrlInfo(){
    YBHttpUtils.getResponseRequest(weChatUrlStr, {"f":"json"},success: (data){
      print("---外面来的URL----$data-");
      setState(() {
        var imageurl = data['cdn_url'];
        _imageUrl = imageurl;
        var title = data['title'];
        _title = title;
        var orgin = data['nick_name'];
        _orgin = orgin;
        var content = data['content_noencode'];
        _contentStr = content;
        _editingController.text = orgin;
      });


    },fail: (error){
      print("---外面来的URL----$error-");
    });
  }

  void _getWenZhangeInfo(String content,String title,String imageStr,String origin,String originUrl){

    Map<String,dynamic> wenZhangMap = Map<String,dynamic>();
    wenZhangMap["sysCode"] = "xg_youbang";
    if(content != null) {
      wenZhangMap["content"] = content;
    }
    if(imageStr != null){
      wenZhangMap["imageUrl"] = imageStr;
    }
    if(origin != null){
      wenZhangMap["origin"] = origin;
    }
    if (title != null){
      wenZhangMap["title"] = title;
    }
    if (originUrl != null){
      wenZhangMap["originUrl"] = originUrl;
    }

    print("object------$wenZhangMap");

    YBBaseWorkTaiNetWork.workTaisaveWenZhangStrData(wenZhangMap,success: (data){
      print("----result-$data--");
     YBBaseModel baseModel = YBBaseModel.fromJson(data);
     if (baseModel.code == 0){
        EasyLoading.showSuccess("发布成功");
        YBNavigatorUtil.goBack(context);
     }else{
        EasyLoading.show(status:'发布失败');
     }
    },fail: (error){
      print("----result-$error--");
    });
  }


}


